//
//  ClockDialVC.m
//  UILbrary
//
//  Created by mac on 16/8/15.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ClockDialVC.h"
#import "UIColor+expanded.h"
#import "ClockDialView.h"
@interface ClockDialVC ()
@property (nonatomic,strong) ClockDialView *clockDiaView;
@end

@implementation ClockDialVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.clockDiaView = [[ClockDialView alloc] initWithFrame:self.view.frame];
    self.clockDiaView.backgroundColor = [UIColor grayColor];
    self.view.backgroundColor = [UIColor grayColor];
    self.clockDiaView.speedValue = 120;
    [self.clockDiaView layoutIfNeeded];
    [self.view addSubview:self.clockDiaView];

    [NSTimer scheduledTimerWithTimeInterval:2.0
                                     target:self
                                   selector:@selector(gaugeUpdateTimer:)
                                   userInfo:nil
                                    repeats:YES];
    
}


-(void)gaugeUpdateTimer:(NSTimer *)timer
{
    self.clockDiaView.speedValue = rand()%(220);
    CGFloat x = arc4random() % 100;
   
//    [self.clockDiaView setNeedsDisplay];
    [self.clockDiaView animalWith:x];
    
}


//未封装的代码
- (void)createSubViews{
#pragma  mark - 最外层蓝色的圆弧
    self.view.backgroundColor = [UIColor darkGrayColor];
    CGPoint center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
    //贝斯曲线 的路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:150 startAngle:M_PI* (-195)/180  endAngle:M_PI * (15)/180 clockwise:YES];
    
    //用来画曲线
    CAShapeLayer * layer = [CAShapeLayer layer];
    //填充颜色  当设为 橘黄色的时候 如下图  设为透明为下图
    layer.fillColor = [UIColor clearColor].CGColor;
    //圆弧线条颜色
    layer.strokeColor = [UIColor colorWithHexString:@"329cd8"].CGColor;//[UIColor colorWithRed:0.12 green:0.52 blue:0.23 alpha:1].CGColor;
    //圆弧线条两端的模式，kCALineCapSquare 直角   kCALineCapRound 圆角
    layer.lineCap = kCALineCapRound;
    //圆弧线段的宽度
    layer.lineWidth = 2;
    //给layer 添加路径
    layer.path = [path CGPath];
    [self.view.layer addSublayer:layer];
    
    
    
    
#pragma mark - 白色的刻度
    for (int i = 0; i<45; i++) {
        
        CGFloat perAngle = 210.f/44.f;
        CGFloat startAngle = M_PI * (-195 + i * perAngle)/180;
        CGFloat textAngle = M_PI * (195 - i * perAngle)/180;
        CGFloat endAngle = startAngle + M_PI * (perAngle/5)/180;
        UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center radius:125 startAngle:startAngle endAngle:endAngle clockwise:YES];
        CAShapeLayer *layer1 = [CAShapeLayer layer];
        layer1.fillColor = [UIColor clearColor].CGColor;
        layer1.strokeColor = [UIColor whiteColor].CGColor;
        layer1.lineCap = kCALineCapButt;//如果设为 kCALineCapSquare 则没有间距 kCALineCapRound 也会连在一起，不过会有凹凸的连续
        layer1.lineWidth = 5.0;
        if (i%4 == 0) {
            //长刻度
            layer1.strokeColor = [UIColor whiteColor].CGColor;
            layer1.lineWidth = 10.0;
            
            CGPoint point = [self calcCircleCoordinateWithCenter:center andWithAngle:textAngle andWithRadius:110];
            NSString *valueString = [NSString stringWithFormat:@"%d",i * 5];
            
            UIFont* font = [UIFont fontWithName:@"Helvetica-Bold" size:7];
            //刻度值
            UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(point.x - 5, point.y - 5, 12, 12)];
            text.text = valueString;
            text.font = font;
            text.textColor = [UIColor whiteColor];//[UIColor colorWithRed:0.54 green:0.78 blue:0.91 alpha:1.0];
            text.textAlignment = NSTextAlignmentCenter;
            [self.view addSubview:text];
        }
        layer1.path = [path1 CGPath];
        [self.view.layer addSublayer:layer1];
    }
    
    
    //表盘中心的圆弧
    //贝斯曲线 的路径
    UIBezierPath *centerCirclePath = [UIBezierPath bezierPathWithArcCenter:center radius:20 startAngle:0  endAngle:M_PI * 2 clockwise:YES];
    
    //用来画曲线
    CAShapeLayer * CirclLayer = [CAShapeLayer layer];
    //填充颜色  当设为 橘黄色的时候 如下图  设为透明为下图
    CirclLayer.fillColor = [UIColor clearColor].CGColor;
    //圆弧线条颜色
    CirclLayer.strokeColor = [UIColor redColor].CGColor;//[UIColor colorWithRed:0.12 green:0.52 blue:0.23 alpha:1].CGColor;
    //圆弧线条两端的模式，kCALineCapSquare 直角   kCALineCapRound 圆角
    CirclLayer.lineCap = kCALineCapRound;
    //圆弧线段的宽度
    CirclLayer.lineWidth = 2;
    //给layer 添加路径
    CirclLayer.path = [centerCirclePath CGPath];
    [self.view.layer addSublayer:CirclLayer];
    
    
    
    //第二个表盘中心的圆弧2
    //贝斯曲线 的路径
    UIBezierPath *centerCirclePath1 = [UIBezierPath bezierPathWithArcCenter:center radius:10 startAngle:0  endAngle:M_PI * 2 clockwise:YES];
    
    
    //用来画曲线
    CAShapeLayer * CirclLayer1 = [CAShapeLayer layer];
    //填充颜色  当设为 橘黄色的时候 如下图  设为透明为下图
    CirclLayer1.fillColor = [UIColor redColor].CGColor;
    //圆弧线条颜色
    CirclLayer1.strokeColor = [UIColor redColor].CGColor;//[UIColor colorWithRed:0.12 green:0.52 blue:0.23 alpha:1].CGColor;
    //圆弧线条两端的模式，kCALineCapSquare 直角   kCALineCapRound 圆角
    CirclLayer1.lineCap = kCALineCapRound;
    //圆弧线段的宽度
    CirclLayer1.lineWidth = 2;
    //给layer 添加路径
    CirclLayer1.path = [centerCirclePath1 CGPath];
    [self.view.layer addSublayer:CirclLayer1];
    
    
    
    
    
#pragma mark - 画表针
    UIBezierPath *needlePath = [UIBezierPath bezierPath];
    [needlePath moveToPoint:center];
    CGFloat needleWidth = 10;
    CGFloat needleHeight = 120;
    //needleWidth needleWith
    
    //表尖坐标  只要控制好表尖坐标 则这个表针就可以动了
    
    CGFloat speedValue = 45;
    //根据速度算出此速度在表盘上面的弧度
    CGFloat needleTipAngle = (195- speedValue/5 * 210.0/44.0) /180.0 * M_PI;//195/180.0 * M_PI
    CGPoint needleTipPoint = [self calcCircleCoordinateWithCenter:center andWithAngle:needleTipAngle andWithRadius:needleHeight];
    [needlePath addLineToPoint:CGPointMake(center.x - needleWidth, center.y)];
    //    [needlePath addLineToPoint:CGPointMake(center.x, center.y - needleHeight)];
    [needlePath addLineToPoint:needleTipPoint];
    [needlePath addLineToPoint:CGPointMake(center.x + needleWidth, center.y)];
    //    [needlePath closePath];
    //    [[UIColor redColor] setFill];
    
    
    //    [needlePath fill];
    CAShapeLayer *needleLayer = [CAShapeLayer layer];
    needleLayer.fillColor = [UIColor redColor].CGColor;
    needleLayer.strokeColor = [UIColor redColor].CGColor;
    needleLayer.lineCap = kCALineCapRound;
    needleLayer.lineWidth = 0;
    needleLayer.path = [needlePath CGPath];
    [self.view.layer addSublayer:needleLayer];
}
- (CGPoint) calcCircleCoordinateWithCenter:(CGPoint) center  andWithAngle : (CGFloat) angle andWithRadius: (CGFloat) radius{
    CGFloat x2 = radius*cosf(angle);
    CGFloat y2 = radius*sinf(angle);
    return CGPointMake(center.x+x2, center.y-y2);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
